package com.yubest.mqtt.controller;

import com.yubest.mqtt.handler.MqttProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 发送数据
 *
 * @Author: hweiyu
 * @Date: 2022/11/11 10:32
 */
@RestController
public class SendController {

    @Value("${mqtt.producer.defaultTopic}")
    private String topic;

    @Autowired
    private MqttProducer mqttProducer;

    @GetMapping(value = "/send")
    public Object send() {
        for (int i = 0; i < 5; i++) {
            mqttProducer.send(topic, MessageBuilder.withPayload("你好" + i).build());
        }
        return "ok";
    }
}
